import 'package:flutter/material.dart';
import 'package:flutter_snail/pages/category/category_page.dart';
import 'package:flutter_snail/pages/desk/desk_page.dart';
import 'package:flutter_snail/pages/home/snail_navigation_bar.dart';
import 'package:flutter_snail/pages/mine/mine_page.dart';
import 'package:flutter_snail/pages/reading/reading_page.dart';
import 'package:flutter_snail/widgets/tool/base_state.dart';
import 'package:flutter_snail/widgets/tool/keep_alive_wrapper.dart';

/// 首页
class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends BaseState<HomePage> {
  int currentIndex = 0;

  late PageController _controller;

  @override
  void initState() {
    super.initState();
    _controller = PageController(initialPage: currentIndex);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _buildBody(),
      bottomNavigationBar: bottomBar,
    );
  }

  Widget _buildBody() {
    return PageView(
      controller: _controller,
      physics: const NeverScrollableScrollPhysics(),
      children: const [
        KeepAliveWrapper(child: ReadingPage()),
        KeepAliveWrapper(child: CategoryPage()),
        KeepAliveWrapper(child: DeskPage()),
        KeepAliveWrapper(child: MinePage()),
      ],
    );
  }

  /// 底部导航栏
  Widget get bottomBar => SnailNavigationBar(
        items: items,
        currentIndex: currentIndex,
        changed: onChange,
      );

  List<BottomNavigationBarItem> get items => const [
        BottomNavigationBarItem(
            icon: Icon(Icons.people_alt_outlined), label: '领读', tooltip: '领读'),
        BottomNavigationBarItem(
            icon: Icon(Icons.category_outlined), label: '分类', tooltip: '分类'),
        BottomNavigationBarItem(
            icon: Icon(Icons.desk_outlined), label: '书桌', tooltip: '书桌'),
        BottomNavigationBarItem(
            icon: Icon(Icons.man), label: '我', tooltip: '我'),
      ];

  void onChange(int value) {
    currentIndex = value;
    _controller.jumpToPage(currentIndex);
  }
}
